package noobteam.deartrainer.mapper;

import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;

/**
 * @author 徐紫印
 */
@Mapper
public interface ChatMapper {

    /**
     * 插入聊天
     * @param chat 聊天内容，包括
     * @return 返回操作是否完成
     */
    @Insert("INSERT INTO chat (sourceId, sourceType, targetId, targetType, time, content) " +
            "VALUES (#{chat.sourceId}, #{chat.sourceType}, #{chat.targetId}, #{chat.targetType}, #{chat.time}, #{chat.content})")
    public boolean createChat(@Param("chat" )Map<String, String> chat);

    /**
     * 删除对话
     * @param chatId 记录的id
     * @return 操作是否完成
     */
    @Delete("DELETE FROM chat WHERE chatId = #{chatId}")
    public boolean deleteChat(@Param("chatId") Integer chatId);

    /**
     * 获取对话
     * 传入两个用户的ID和TYPE
     * @param map 教练和用户的id
     * @return 返回列表
     */
    @Select("SELECT * FROM chat " +
            "WHERE sourceId = #{js.sourceId} AND sourceType = #{js.sourceType} AND targetId = #{js.targetId} AND targetType = #{js.targetType} " +
            "OR sourceId = #{js.targetId} AND sourceType = #{js.targetType} AND targetId = #{js.sourceId} AND targetType = #{js.sourceType} " +
            "ORDER BY time")
    public List<Map<String, String>> getChatHistory(@Param("js") Map<String, String> map);

    /**
     * 获取所有关于某人的信息
     */
    @Select("SELECT * FROM chat " +
            "WHERE sourceId = #{js.userId} AND sourceType = #{js.userType} " +
            "OR targetId = #{js.userId} AND targetId = #{js.userType}")
    public List<Map<String, Object>> getChatOfUser(@Param("js") Map<String, Object> user);
}
